package com.hunter.api.help.exception;

import java.util.Collection;
import java.util.regex.Pattern;

import cn.hutool.core.collection.CollectionUtil;
import com.hunter.api.help.constant.SysConstants;
import com.hunter.api.help.result.BusinessCode;
import org.apache.commons.lang3.StringUtils;

/**
 * 断言，否则跑出 BusinessException
 * <p>
 *
 * @author Created by lfan on 2020/6/22 17:04
 */
public class BusinessAssert {
    /**
     * 断言对象不是空，当是 String 时，判断是否为 null  ||  空字符串
     *
     * @param object
     * @param businessCode
     */
    public static void notNull(Object object, BusinessCode businessCode) {
        if (object == null) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言是空对象
     *
     * @param object
     * @param businessCode
     */
    public static void isNull(Object object, BusinessCode businessCode) {
        if (object != null) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言不是空字符串
     *
     * @param string
     * @param businessCode
     */
    public static void notNull(String string, BusinessCode businessCode) {
        if (StringUtils.isBlank(string)) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言不是空字符串
     *
     * @param string
     * @param businessCode
     */
    public static void notNull(String string, BusinessCode businessCode,String arg) {
        if (StringUtils.isBlank(string)) {
            String msg = businessCode.getMessage()+arg;
            businessCode.setMessage(msg);
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言是空字符串
     *
     * @param string
     * @param businessCode
     */
    public static void isNull(String string, BusinessCode businessCode) {
        if (StringUtils.isNotBlank(string)) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言字符串长度不超过 maxLength
     *
     * @param string
     * @param maxLength
     * @param businessCode
     */
    public static void notOutOfLength(String string, int maxLength, BusinessCode businessCode) {
        if (string != null && string.length() > maxLength) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言 source 满足 regex 表达式
     *
     * @param source
     * @param regex
     * @param businessCode
     */
    public static void matches(String source, String regex, BusinessCode businessCode) {
        if (!Pattern.matches(regex, source)) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言 source == 0
     *
     * @param source
     * @param businessCode
     */
    public static void isZero(int source, BusinessCode businessCode) {
        if (source != SysConstants.ZERO) {
            throw new BusinessException(businessCode);
        }
    }


    /**
     * 集合size 不能超过最大值
     *
     * @param collection
     * @param maxSize
     * @param businessCode
     * @param <T>
     */
    public static <T> void collectionSizeNotOutMax(Collection<T> collection, int maxSize, BusinessCode businessCode) {
        if (CollectionUtil.isNotEmpty(collection) && collection.size() > maxSize) {
            throw new BusinessException(businessCode);
        }
    }


    /**
     * 断言一定为true
     *
     * @param flag
     * @param businessCode
     */
    public static void isTrue(boolean flag, BusinessCode businessCode) {
        if (!flag) {
            throw new BusinessException(businessCode);
        }
    }

    /**
     * 断言一定为false
     *
     * @param flag
     * @param businessCode
     */
    public static void isFalse(boolean flag, BusinessCode businessCode) {
        isTrue(!flag, businessCode);
    }

    private BusinessAssert() {
    }
}
